package C002;

import java.util.Arrays;

/**
 * ClassName: SocialTest
 * Package: C001
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/26 00:02
 * @Version 1.0
 * @Since 1.0
 */
public class SocialTest {

    public static void main(String[] args) {
        int num = 100;  // 一共多少人
        int count = 10000000; // 进行分配次数

        int[] arr = new int[num];
        Arrays.fill(arr, 100); // 填充100至每个元素

        boolean[] isWealth = new boolean[num]; // 是否有财富
        for (int i = 0; i < count; i++) {
            Arrays.fill(isWealth,false);
            // 初始化-判断是否有财富
            for (int j = 0; j < arr.length; j++) {
                if(arr[j] > 0) {
                    isWealth[j] = true;
                }
            }

            for (int j = 0; j < arr.length; j++) {
                if(!isWealth[j]) {
                    continue;
                }
                // 分配资源
                // 随机选取下标
                int index = -1;
                do {
                    index = (int) (Math.random() * arr.length);
                } while (index == j);
                arr[j] --;
                arr[index] ++;
            }
        }

        // 计算基尼系数
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            if(i != 0 && i % 10 == 0) {
                System.out.println();
            }
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println(giniCoefficient(arr));
    }

    /**
     * 计算基尼系数
     */
    public static double giniCoefficient(int[] arr) {
        int welathAll = 0;
        int absoultNum = 0;
        for (int i = 0; i < arr.length; i++) {
            welathAll += arr[i];
            for (int j = 0; j < arr.length; j++) {
                absoultNum += Math.abs(arr[i] - arr[j]);
            }
        }
        return  absoultNum / (2.0 * arr.length * welathAll);
    }

}
